home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gsmemory.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  10.8 KB  |  299 lines

  1. /* Copyright (C) 1993, 1996, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gsmemory.h,v 1.2 2000/09/19 19:00:30 lpd Exp $ */
  20. /* Client interface for memory allocation */
  21.  
  22. /*
  23.  * The allocator knows about two basic kinds of memory: objects, which are
  24.  * aligned and cannot have pointers to their interior, and strings, which
  25.  * are not aligned and which can have interior references.
  26.  *
  27.  * The standard allocator is designed to interface to a garbage collector,
  28.  * although it does not include or call one.  The allocator API recognizes
  29.  * that the garbage collector may move objects, relocating pointers to them;
  30.  * the API provides for allocating both movable (the default) and immovable
  31.  * objects.  Clients must not attempt to resize immovable objects, and must
  32.  * not create references to substrings of immovable strings.
  33.  */
  34.  
  35. #ifndef gsmemory_INCLUDED
  36. #  define gsmemory_INCLUDED
  37.  
  38. #include "gsmemraw.h"
  39.  
  40. /* Define the opaque type for a structure descriptor. */
  41. typedef struct gs_memory_struct_type_s gs_memory_struct_type_t;
  42. typedef const gs_memory_struct_type_t *gs_memory_type_ptr_t;
  43.  
  44. /* Define the opaque type for an allocator. */
  45. /* (The actual structure is defined later in this file.) */
  46. #ifndef gs_memory_DEFINED
  47. #  define gs_memory_DEFINED
  48. typedef struct gs_memory_s gs_memory_t;
  49. #endif
  50.  
  51. /* Define the opaque type for a pointer type. */
  52. typedef struct gs_ptr_procs_s gs_ptr_procs_t;
  53. typedef const gs_ptr_procs_t *gs_ptr_type_t;
  54.  
  55. /* Define the opaque type for a GC root. */
  56. typedef struct gs_gc_root_s gs_gc_root_t;
  57.  
  58.     /* Accessors for structure types. */
  59.  
  60. typedef client_name_t struct_name_t;
  61.  
  62. /* Get the size of a structure from the descriptor. */
  63. uint gs_struct_type_size(P1(gs_memory_type_ptr_t));
  64.  
  65. /* Get the name of a structure from the descriptor. */
  66. struct_name_t gs_struct_type_name(P1(gs_memory_type_ptr_t));
  67.  
  68. #define gs_struct_type_name_string(styp)\
  69.   ((const char *)gs_struct_type_name(styp))
  70.  
  71. /*
  72.  * Define the memory manager procedural interface.
  73.  */
  74. typedef struct gs_memory_procs_s {
  75.  
  76.     gs_raw_memory_procs(gs_memory_t);    /* defined in gsmemraw.h */
  77.  
  78.     /* Redefine inherited procedures with the new allocator type. */
  79.  
  80. #define gs_memory_proc_alloc_bytes(proc)\
  81.   gs_memory_t_proc_alloc_bytes(proc, gs_memory_t)
  82. #define gs_memory_proc_resize_object(proc)\
  83.   gs_memory_t_proc_resize_object(proc, gs_memory_t)
  84. #define gs_memory_proc_free_object(proc)\
  85.   gs_memory_t_proc_free_object(proc, gs_memory_t)
  86. #define gs_memory_proc_stable(proc)\
  87.   gs_memory_t_proc_stable(proc, gs_memory_t)
  88. #define gs_memory_proc_status(proc)\
  89.   gs_memory_t_proc_status(proc, gs_memory_t)
  90. #define gs_memory_proc_free_all(proc)\
  91.   gs_memory_t_proc_free_all(proc, gs_memory_t)
  92. #define gs_memory_proc_consolidate_free(proc)\
  93.   gs_memory_t_proc_consolidate_free(proc, gs_memory_t)
  94.  
  95.     /*
  96.      * Allocate possibly movable bytes.  (We inherit allocating immovable
  97.      * bytes from the raw memory allocator.)
  98.      */
  99.  
  100. #define gs_alloc_bytes(mem, nbytes, cname)\
  101.   (*(mem)->procs.alloc_bytes)(mem, nbytes, cname)
  102.     gs_memory_proc_alloc_bytes((*alloc_bytes));
  103.  
  104.     /*
  105.      * Allocate a structure.
  106.      */
  107.  
  108. #define gs_memory_proc_alloc_struct(proc)\
  109.   void *proc(P3(gs_memory_t *mem, gs_memory_type_ptr_t pstype,\
  110.     client_name_t cname))
  111. #define gs_alloc_struct(mem, typ, pstype, cname)\
  112.   (typ *)(*(mem)->procs.alloc_struct)(mem, pstype, cname)
  113.     gs_memory_proc_alloc_struct((*alloc_struct));
  114. #define gs_alloc_struct_immovable(mem, typ, pstype, cname)\
  115.   (typ *)(*(mem)->procs.alloc_struct_immovable)(mem, pstype, cname)
  116.     gs_memory_proc_alloc_struct((*alloc_struct_immovable));
  117.  
  118.     /*
  119.      * Allocate an array of bytes.
  120.      */
  121.  
  122. #define gs_memory_proc_alloc_byte_array(proc)\
  123.   byte *proc(P4(gs_memory_t *mem, uint num_elements, uint elt_size,\
  124.     client_name_t cname))
  125. #define gs_alloc_byte_array(mem, nelts, esize, cname)\
  126.   (*(mem)->procs.alloc_byte_array)(mem, nelts, esize, cname)
  127.     gs_memory_proc_alloc_byte_array((*alloc_byte_array));
  128. #define gs_alloc_byte_array_immovable(mem, nelts, esize, cname)\
  129.   (*(mem)->procs.alloc_byte_array_immovable)(mem, nelts, esize, cname)
  130.     gs_memory_proc_alloc_byte_array((*alloc_byte_array_immovable));
  131.  
  132.     /*
  133.      * Allocate an array of structures.
  134.      */
  135.  
  136. #define gs_memory_proc_alloc_struct_array(proc)\
  137.   void *proc(P4(gs_memory_t *mem, uint num_elements,\
  138.     gs_memory_type_ptr_t pstype, client_name_t cname))
  139. #define gs_alloc_struct_array(mem, nelts, typ, pstype, cname)\
  140.   (typ *)(*(mem)->procs.alloc_struct_array)(mem, nelts, pstype, cname)
  141.     gs_memory_proc_alloc_struct_array((*alloc_struct_array));
  142. #define gs_alloc_struct_array_immovable(mem, nelts, typ, pstype, cname)\
  143.  (typ *)(*(mem)->procs.alloc_struct_array_immovable)(mem, nelts, pstype, cname)
  144.     gs_memory_proc_alloc_struct_array((*alloc_struct_array_immovable));
  145.  
  146.     /*
  147.      * Get the size of an object (anything except a string).
  148.      */
  149.  
  150. #define gs_memory_proc_object_size(proc)\
  151.   uint proc(P2(gs_memory_t *mem, const void *obj))
  152. #define gs_object_size(mem, obj)\
  153.   (*(mem)->procs.object_size)(mem, obj)
  154.     gs_memory_proc_object_size((*object_size));
  155.  
  156.     /*
  157.      * Get the type of an object (anything except a string).
  158.      * The value returned for byte objects is useful only for
  159.      * printing.
  160.      */
  161.  
  162. #define gs_memory_proc_object_type(proc)\
  163.   gs_memory_type_ptr_t proc(P2(gs_memory_t *mem, const void *obj))
  164. #define gs_object_type(mem, obj)\
  165.   (*(mem)->procs.object_type)(mem, obj)
  166.     gs_memory_proc_object_type((*object_type));
  167.  
  168.     /*
  169.      * Allocate a string (unaligned bytes).
  170.      */
  171.  
  172. #define gs_memory_proc_alloc_string(proc)\
  173.   byte *proc(P3(gs_memory_t *mem, uint nbytes, client_name_t cname))
  174. #define gs_alloc_string(mem, nbytes, cname)\
  175.   (*(mem)->procs.alloc_string)(mem, nbytes, cname)
  176.     gs_memory_proc_alloc_string((*alloc_string));
  177. #define gs_alloc_string_immovable(mem, nbytes, cname)\
  178.   (*(mem)->procs.alloc_string_immovable)(mem, nbytes, cname)
  179.     gs_memory_proc_alloc_string((*alloc_string_immovable));
  180.  
  181.     /*
  182.      * Resize a string.  The specification is the same as resize_object
  183.      * (in gsmemraw.h), except that the element size is always a byte.
  184.      */
  185.  
  186. #define gs_memory_proc_resize_string(proc)\
  187.   byte *proc(P5(gs_memory_t *mem, byte *data, uint old_num, uint new_num,\
  188.     client_name_t cname))
  189. #define gs_resize_string(mem, data, oldn, newn, cname)\
  190.   (*(mem)->procs.resize_string)(mem, data, oldn, newn, cname)
  191.     gs_memory_proc_resize_string((*resize_string));
  192.  
  193.     /*
  194.      * Free a string.
  195.      */
  196.  
  197. #define gs_memory_proc_free_string(proc)\
  198.   void proc(P4(gs_memory_t *mem, byte *data, uint nbytes,\
  199.     client_name_t cname))
  200. #define gs_free_string(mem, data, nbytes, cname)\
  201.   (*(mem)->procs.free_string)(mem, data, nbytes, cname)
  202.     gs_memory_proc_free_string((*free_string));
  203.  
  204.     /*
  205.      * Register a root for the garbage collector.  root = NULL
  206.      * asks the memory manager to allocate the root object
  207.      * itself (immovable, in the manager's parent): this is the usual
  208.      * way to call this procedure.
  209.      */
  210.  
  211. #define gs_memory_proc_register_root(proc)\
  212.   int proc(P5(gs_memory_t *mem, gs_gc_root_t *root, gs_ptr_type_t ptype,\
  213.     void **pp, client_name_t cname))
  214. #define gs_register_root(mem, root, ptype, pp, cname)\
  215.   (*(mem)->procs.register_root)(mem, root, ptype, pp, cname)
  216.     gs_memory_proc_register_root((*register_root));
  217.  
  218.     /*
  219.      * Unregister a root.  The root object itself will be freed iff
  220.      * it was allocated by gs_register_root.
  221.      */
  222.  
  223. #define gs_memory_proc_unregister_root(proc)\
  224.   void proc(P3(gs_memory_t *mem, gs_gc_root_t *root, client_name_t cname))
  225. #define gs_unregister_root(mem, root, cname)\
  226.   (*(mem)->procs.unregister_root)(mem, root, cname)
  227.     gs_memory_proc_unregister_root((*unregister_root));
  228.  
  229.     /*
  230.      * Enable or disable the freeing operations: when disabled,
  231.      * these operations return normally but do nothing.  The
  232.      * garbage collector and the PostScript interpreter
  233.      * 'restore' operator need to temporarily disable the
  234.      * freeing functions of (an) allocator(s) while running
  235.      * finalization procedures.
  236.      */
  237.  
  238. #define gs_memory_proc_enable_free(proc)\
  239.   void proc(P2(gs_memory_t *mem, bool enable))
  240. #define gs_enable_free(mem, enable)\
  241.   (*(mem)->procs.enable_free)(mem, enable)
  242.     gs_memory_proc_enable_free((*enable_free));
  243.  
  244. } gs_memory_procs_t;
  245.  
  246. /*
  247.  * Define versions of the freeing procedures that are applicable even if the
  248.  * pointer is declared as const T *.  These are intended for use where a
  249.  * structure contains a pointer member whose referent is declared as const
  250.  * because it is const for all ordinary clients.
  251.  */
  252. void gs_free_const_object(P3(gs_memory_t *mem, const void *data,
  253.                  client_name_t cname));
  254. void gs_free_const_string(P4(gs_memory_t *mem, const byte *data, uint nbytes,
  255.                  client_name_t cname));
  256.  
  257. /*
  258.  * Either allocate (if obj == 0) or resize (if obj != 0) a structure array.
  259.  * If obj != 0, pstype is used only for checking (in DEBUG configurations).
  260.  */
  261. void *gs_resize_struct_array(P5(gs_memory_t *mem, void *obj, uint num_elements,
  262.                 gs_memory_type_ptr_t pstype,
  263.                 client_name_t cname));
  264.  
  265. /* Register a structure root.  This just calls gs_register_root. */
  266. int gs_register_struct_root(P4(gs_memory_t *mem, gs_gc_root_t *root,
  267.                    void **pp, client_name_t cname));
  268.  
  269. /* Define no-op freeing procedures for use by enable_free. */
  270. gs_memory_proc_free_object(gs_ignore_free_object);
  271. gs_memory_proc_free_string(gs_ignore_free_string);
  272.  
  273. /* Define a no-op consolidation procedure. */
  274. gs_memory_proc_consolidate_free(gs_ignore_consolidate_free);
  275.  
  276. /*
  277.  * Allocate a structure using a "raw memory" allocator.  Note that this does
  278.  * not retain the identity of the structure.  Note also that it returns a
  279.  * void *, and does not take the type of the returned pointer as a
  280.  * parameter.
  281.  */
  282. void *gs_raw_alloc_struct_immovable(P3(gs_raw_memory_t * rmem,
  283.                        gs_memory_type_ptr_t pstype,
  284.                        client_name_t cname));
  285.  
  286. /*
  287.  * Define an abstract allocator instance.
  288.  * Subclasses may have state as well.
  289.  * Note that the initial part of this must match gs_raw_memory_t.
  290.  */
  291. #define gs_memory_common\
  292.     gs_memory_t *stable_memory;\
  293.     gs_memory_procs_t procs
  294. struct gs_memory_s {
  295.     gs_memory_common;
  296. };
  297.  
  298. #endif /* gsmemory_INCLUDED */
  299.